import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/wait_task_count_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/norderauditresult_info.dart';

//非预约审核
class TabNorderauditresultTwo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TabNorderauditresultTwoState();
  }
}

class TabNorderauditresultTwoState extends State<TabNorderauditresultTwo>
    with AutomaticKeepAliveClientMixin {
  bool show = true;
  InvListEntity bean;
  List<InvListData> exhibitionList = new List();
  List<InvListData> unauditedList = new List();
  List<InvListData> auditPassList = new List();
  List<InvListData> auditFailedList = new List();
  List<InvListData> obsolete = new List();

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    int tabIndex=Provider.of<NorderauditresultInfo>(context).tabIndex;
    int selected=Provider.of<NorderauditresultInfo>(context).selected;
    print(tabIndex);
    print(selected);
    print("第二页");
    if(tabIndex==1){
      if(bean!=null){
        switch (selected) {
          case 0:
            exhibitionList = unauditedList;
            break;
          case 1:
            exhibitionList = auditPassList;
            break;
          case 2:
            exhibitionList = auditFailedList;
            break;
          case 3:
            exhibitionList = obsolete;
            break;
          default:
            exhibitionList = bean.datas;
            break;
        }
      }
    }
    return LoadBuilder(
        visibility: show,
        emptyVisibility: !show&&exhibitionList.length==0,
        builder: RefreshIndicator(
          child: Scrollbar(
              child: ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return TextCartItem(
                      data: exhibitionList,
                      index: index,
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(color: Colors.black26);
                  },
                  itemCount: exhibitionList.length)),
          onRefresh: getData,
        ));
  }

  @override
  bool get wantKeepAlive => true;

  Future getData() async {
    show = true;
    unauditedList.clear();
    auditPassList.clear();
    auditFailedList.clear();
    exhibitionList.clear();
    obsolete.clear();
    InternetAPI.getInstance().get("OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask",
            (data) {
            bean = InvListEntity.fromJson(data);
            for (int i = 0; i < bean.datas.length; i++) {
              if (bean.datas.elementAt(i).state == "未分配") {
                unauditedList.add(bean.datas[i]);
              } else if (bean.datas.elementAt(i).state == "未审批") {
                unauditedList.add(bean.datas[i]);
              } else if (bean.datas.elementAt(i).state == "未出库") {
                auditPassList.add(bean.datas[i]);
              } else if (bean.datas.elementAt(i).state == "已出库") {
                auditPassList.add(bean.datas[i]);
              } else if (bean.datas.elementAt(i).state == "待执行") {
                auditPassList.add(bean.datas[i]);
              }else if (bean.datas.elementAt(i).state == "已驳回") {
                auditFailedList.add(bean.datas[i]);
              }else if (bean.datas.elementAt(i).state == "已废弃"||bean.datas.elementAt(i).state == "已作废") {
                obsolete.add(bean.datas[i]);
              }
            }
            setState(() {
              show = false;
              exhibitionList=bean.datas;
            });

        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'userId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'pageNo': -1,
          'appointmentmode':10
        });
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<InvListData> data;
  final int index;
  final Function(int index) onTapFuns;

  Widget stateWidget;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  Widget redBox=DecoratedBox(
    decoration: BoxDecoration(color:  Color(0xffcccccc)),
  );

  @override
  Widget build(BuildContext context) {
    if (data.elementAt(index).state == "未分配") {
      stateWidget = Text(
        "未审核",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF00A5F1)),
      );
    } else if (data.elementAt(index).state == "未审批") {
      stateWidget = Text(
        "未审核",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF00A5F1)),
      );
    } else if (data.elementAt(index).state == "未入库" || data.elementAt(index).state == "未出库") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    } else if (data.elementAt(index).state == "已入库" || data.elementAt(index).state == "已出库") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    }else if (data.elementAt(index).state == "待执行") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    } else if (data.elementAt(index).state == "已驳回") {
      stateWidget = Text(
        "审核未通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFFFB4747)),
      );
    }else if (data.elementAt(index).state == "已废弃"||data.elementAt(index).state == "已作废") {
      stateWidget = Text(
        "已废弃",
        style: TextStyle(fontSize: 14.0, color: Color(0xFFFB4747)),
      );
    }
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
            //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("${data.elementAt(index).projectname}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 10, bottom: 10),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("${data.elementAt(index).taskno}"),
                ),
              ),
              Column(
                children: <Widget>[
                  Flex(
                    direction: Axis.horizontal,
                    children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: stateWidget
                      ),
                      Expanded(
                          flex: 2,
                          child: Text(
                            "${data.elementAt(index).creationTime}",
                            textAlign: TextAlign.right,
                            style: TextStyle(
                              color: Color(0xff646464),
                              fontSize: 14,
                            ),
                          )
                      ),
                    ],
                  ),
                ],
              ),
              Offstage(offstage: data.elementAt(index).remark==null,child: Column(
                  children: <Widget>[
                    ConstrainedBox(
                      constraints: BoxConstraints(
                          minWidth: double.infinity, //宽度尽可能大
                          minHeight: 40.0 //最小高度为50像素
                      ),
                      child: Container(
                        decoration: BoxDecoration(
                            color: Color(0xFFE9E9E9),
                            borderRadius: BorderRadius.circular(20)
                        ),
                        margin: EdgeInsets.only(top: 10.0), //容器外补白
                        padding:  EdgeInsets.all(5.0), //容器外补白
                        child: Text(
                            "原因:${data.elementAt(index).remark}",
                            style: TextStyle(
                              color: Color(0xff646464),
                              fontSize: 14,
                            )),
                      ),
                    )
                  ]
              ),)

            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          print("加载中~~~~");
        }
      },
    );
  }
}

